package viettravel.guiandevent;

import java.util.ArrayList;

import viettravel.DAO.ProvinceDAO;
import viettravel.DAO.RegionDAO;
import viettravel.DTO.ViettravelDTO;
import viettravel.common.Constants;
import viettravel.guiandevent.expandable.ExpandableListAdapter;
import viettravel.guiandevent.expandable.RegionExpandable;
import viettravel.guiandevent.place.ListPlaceActivity;

import com.actionbarsherlock.widget.SearchView;

import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListView;

@SuppressLint("ValidFragment")
public class MenuProvincesFragment extends ListFragment implements
		SearchView.OnQueryTextListener, SearchView.OnCloseListener {

	private static final String TAG = "MyLog";
	private View mView;
	private Context mContext;
	private SearchView mSearchView;
	private ExpandableListAdapter mExpandableListAdapter;
	private ExpandableListView mExpandableListView;
	private ArrayList<RegionExpandable> mRegionExpandable = new ArrayList<RegionExpandable>();
	private ViettravelDTO mViettravelDTO;

	public MenuProvincesFragment() {
		// mViettravelDTO = ViettravelDTO.getInstance();
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.layout_province_expandable,
				container, false);
		return mView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		mContext = getActivity();
		// mViettravelDTO = new ViettravelDTO(mContext);
		mViettravelDTO = ViettravelDTO.getInstance();

		SearchManager searchManager = (SearchManager) mContext
				.getSystemService(Context.SEARCH_SERVICE);
		mSearchView = (SearchView) view.findViewById(R.id.provinceFilter);
		mSearchView.setSearchableInfo(searchManager
				.getSearchableInfo(getActivity().getComponentName()));
		mSearchView.setIconifiedByDefault(false);
		mSearchView.setOnQueryTextListener(this);
		mSearchView.setOnCloseListener(this);

		displayList();
		expandAll();
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Log.i(TAG, v.toString());
	}

	private void expandAll() {
		int count = mExpandableListAdapter.getGroupCount();
		for (int i = 0; i < count; i++) {
			mExpandableListView.expandGroup(i);
		}
	}

	// method to expand all groups
	private void displayList() {
		// display the list
		loadData();
		// get reference to the ExpandableListView
		mExpandableListView = (ExpandableListView) mView
				.findViewById(R.id.listProvinceGroup);
		mExpandableListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				ProvinceDAO p = (ProvinceDAO) mExpandableListAdapter.getChild(
						groupPosition, childPosition);
				Intent intent = new Intent(getActivity(),
						ListPlaceActivity.class);
				intent.putExtra(Constants.EXTRA_PROVINCE_ID, p.getProvinceId());
				startActivity(intent);
				// TODO Auto-generated method stub
				return false;
			}
		});
		// create the adapter by passing your ArrayList data
		mExpandableListAdapter = new ExpandableListAdapter(mContext,
				mRegionExpandable);
		// attach the adapter to the list
		mExpandableListView.setAdapter(mExpandableListAdapter);
	}

	private void loadData() {
		ArrayList<RegionDAO> allRegion = mViettravelDTO.findAllRegion();
		for (RegionDAO regionDao : allRegion) {
			ArrayList<ProvinceDAO> provinceList = mViettravelDTO
					.findProvinceByRegionId(regionDao.getRegionId());
			mRegionExpandable
					.add(new RegionExpandable(regionDao, provinceList));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.actionbarsherlock.widget.SearchView.OnCloseListener#onClose()
	 */
	@Override
	public boolean onClose() {
		// TODO Auto-generated method stub
		mExpandableListAdapter.filterData("");
		expandAll();
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.actionbarsherlock.widget.SearchView.OnQueryTextListener#onQueryTextSubmit
	 * (java.lang.String)
	 */
	@Override
	public boolean onQueryTextSubmit(String query) {
		mExpandableListAdapter.filterData(query);
		expandAll();
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.actionbarsherlock.widget.SearchView.OnQueryTextListener#onQueryTextChange
	 * (java.lang.String)
	 */
	@Override
	public boolean onQueryTextChange(String query) {
		mExpandableListAdapter.filterData(query);
		expandAll();
		return false;
	}

}
